package com.itic.system.carrage.templatePiece.service;

import java.util.List;

import com.itic.appbase.framework.utils.NumberStringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.system.carrage.templatePiece.persistence.dao.CarrageTemplatePieceDao;
import com.itic.system.carrage.templatePiece.persistence.model.CarrageTemplatePiece;
import com.itic.system.carrage.templatePiece.persistence.model.CarrageTemplatePieceExample;
import com.itic.system.carrage.templateRegion.persistence.model.Condition;


@Service
@Transactional
public class CarrageTemplatePieceService
     extends CrudService<CarrageTemplatePieceDao,CarrageTemplatePiece,CarrageTemplatePieceExample> {
    @Autowired
    private CarrageTemplatePieceDao carrageTemplatePieceDao;
    
    public String add(String templateId,String pId,String isDefault,Integer firstPcount,
    		Double firstTranfee,Integer addPcount,Double addTranfee){
    	String result = FAILED;
    	if(StringHelper.isNotBlank(templateId) && StringHelper.isNotBlank(pId) && firstPcount != null && firstTranfee != null
    			&& addPcount != null && addTranfee != null){
    		CarrageTemplatePiece piece = new CarrageTemplatePiece();
    		piece.setAddPieceCount(addPcount);
    		piece.setAddPieceFee(addTranfee);
    		piece.setCarrageTemplateId(templateId);
    		piece.setId(pId);
    		piece.setIsDefault(isDefault);
    		piece.setFirstPieceCount(firstPcount);
    		piece.setFirstPieceFee(firstTranfee);
    		result = this.add(piece);
    	}
    	return result;
    }
    /**
     * 根据模板id查数据
     * @param templateId
     * @return
     */
    public List<CarrageTemplatePiece> getBytemplateId(String templateId){
    	List<CarrageTemplatePiece> list = this.carrageTemplatePieceDao.getBytemplateId(templateId);
    	return list;
    }
    
    public void deleteByCondition(Condition condition){
    	this.carrageTemplatePieceDao.delBypieceId(condition);
    }
    
    public void deleteBytemplateId(String templateId){
    	this.deleteBytemplateId(templateId);
    }

	/**
	 * 查询运费模板默认的续重费
	 * @param templateId
	 * @return
	 */
	public CarrageTemplatePiece getDefaultByTemplateId(String templateId){
		CarrageTemplatePiece carrageTemplatePiece = this.carrageTemplatePieceDao.getDefaultByTemplateId(templateId);
		return carrageTemplatePiece;
    }

	/**
	 * 根据续重Id以及数量计算
	 * @param tempaltePiectId
	 * @param count
	 * @return
	 */
	public double calFeeByTemplatePiectIdAndCount(String tempaltePiectId,int count){
		double fee = 0.0;
		CarrageTemplatePiece carrageTemplatePiece = this.get(tempaltePiectId);
		if(carrageTemplatePiece!=null){
			//首重个数
			int firstCount = carrageTemplatePiece.getFirstPieceCount()==null?1:carrageTemplatePiece.getFirstPieceCount();
			//首重费用
			double firstFee = carrageTemplatePiece.getFirstPieceFee()==null?0:carrageTemplatePiece.getFirstPieceFee();
			//续重个数
			int addCount = carrageTemplatePiece.getAddPieceCount()==null?1:carrageTemplatePiece.getAddPieceCount();
			//续重费用
			double addFee = carrageTemplatePiece.getAddPieceFee() ==null?0:carrageTemplatePiece.getAddPieceFee();
			fee = fee + firstFee;
			//购买个数大于首重个数
			if(count >firstCount){
				//差异个数
				int differCount = count - firstCount;
				//续重个数的倍数 向上取整用Math.ceil(double a)
				String divCount = NumberStringHelper.divString(Integer.toString(differCount),Integer.toString(addCount),2);
				double multipleCount = Math.ceil(Double.parseDouble(divCount));
				fee = fee + (addFee * multipleCount);
			}
		}
		return fee;
	}
}